package com.hs.panghu.dao.impl;

import com.hs.panghu.dao.ShopDao;
import com.hs.panghu.entity.Shop;
import com.hs.panghu.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ShopDaoImpl implements ShopDao {

    @Override
    public int insertShop(Shop shop) {
        String sql = "insert into shop values(null,now(),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object obj[] = {
                shop.getConcernStatus(),
                shop.getDistance(),
                shop.getSalesVolume(),
                shop.getShopAccount(),
                shop.getShopAddress(),
                shop.getShopCategoryList(),
                shop.getShopCity(),
                shop.getShopCommunitySort(),
                shop.getShopDescribe(),
                shop.getShopDistrict(),
                shop.getShopGoodsList(),
                shop.getShopImage(),
                shop.getShopLatitude(),
                shop.getShopLongitude(),
                shop.getShopName(),
                shop.getShopOrderCount(),
                shop.getShopPassword(),
                shop.getShopPhone(),
                shop.getShopProvince(),
                shop.getShopSort(),
                shop.getShopStatus(),
                shop.getShopUserId()
        };
        int row = JDBCDruidUtil.update(sql, obj);
        return row;
    }

    @Override
    public int updateShop(Shop shop) {
        String sql = "update shop set addTime=now(),concernStatus=?,distance=?,salesVolume=?,shopAccount=?,shopAddress=?,shopCategoryList=?,shopCity=?,shopCommunitySort=?,shopDescribe=?,shopDistrict=?,shopGoodsList=?,shopImage=?,shopLatitude=?,shopLongitude=?,shopName=?,shopOrderCount=?,shopPassword=?,shopPhone=?,shopProvince=?,shopSort=?,shopStatus=?,shopUserId=? where shopId = ?";
        Object obj[] = {
            shop.getConcernStatus(),
            shop.getDistance(),
            shop.getSalesVolume(),
            shop.getShopAccount(),
            shop.getShopAddress(),
            shop.getShopCategoryList(),
            shop.getShopCity(),
            shop.getShopCommunitySort(),
            shop.getShopDescribe(),
            shop.getShopDistrict(),
            shop.getShopGoodsList(),
            shop.getShopImage(),
            shop.getShopLatitude(),
            shop.getShopLongitude(),
            shop.getShopName(),
            shop.getShopOrderCount(),
            shop.getShopPassword(),
            shop.getShopPhone(),
            shop.getShopProvince(),
            shop.getShopSort(),
            shop.getShopStatus(),
            shop.getShopUserId(),
            shop.getShopId()
        };
        int row = JDBCDruidUtil.update(sql, obj);
        System.out.println(row);
        return row;
    }

    @Override
    public int deleteShop(Integer shop) {
        return 0;
    }

    @Override
    public List<Shop> selectShops(Map<String, String> map) {
        List<Shop> list = new ArrayList<>();
        //动态sql和动态数组
        StringBuffer stb = new StringBuffer("select * from shop where 1=1 ");
        List param = new ArrayList();
        //判断name是否有数据，如果有，则拼接
        String keyword = map.get("keyword");
        if(keyword!=null && !"".equals(keyword)){
            stb.append(" and ShopName like ? ");
            param.add("%"+keyword+"%");
        }
        //判断状态
        String shopStatus = map.get("shopStatus");
        System.out.println(shopStatus);
        if(shopStatus!=null && !"".equals(shopStatus)){
            stb.append(" and shopStatus = ? ");
            param.add(shopStatus);
        }
        System.out.println(shopStatus);
        //判断手机号
        String phone = map.get("phone");
        if(phone!=null && !"".equals(phone)){
            stb.append(" and shopPhone like ? ");
            param.add("%"+phone+"%");
        }
        //判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if(startTime!=null && !"".equals(startTime)){
            stb.append(" and addTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
        //分页处理
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        stb.append(" order by shopId desc limit ?,? ");
        param.add((pageNum-1)*pageSize);
        param.add(pageSize);
        String sql = stb.toString();
        Object object[] = param.toArray();
        System.out.println(sql);
        ResultSet resultSet = JDBCDruidUtil.query(sql, object);
        try {
            while (resultSet.next()){
                Shop shop = new Shop(
                        //
                        resultSet.getInt(1),
                        resultSet.getDate(2),
                        resultSet.getInt(3),
                        resultSet.getString(4),
                        resultSet.getString(5),
                        resultSet.getString(6),
                        resultSet.getString(7),
                        resultSet.getString(8),
                        resultSet.getString(9),
                        resultSet.getString(10),
                        resultSet.getString(11),
                        resultSet.getString(12),
                        resultSet.getString(13),
                        resultSet.getString(14),
                        resultSet.getString(15),
                        resultSet.getString(16),
                        resultSet.getString(17),
                        resultSet.getInt(18),
                        resultSet.getString(19),
                        resultSet.getString(20),
                        resultSet.getString(21),
                        resultSet.getInt(22),
                        resultSet.getInt(23),
                        resultSet.getInt(24)
                );
                list.add(shop);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCDruidUtil.close();
        }
        return list;
    }
    @Override
    public int selectShopCount(Map<String, String> map) {
        int count = 0;
        //动态sql和动态数组
        StringBuffer stb = new StringBuffer("select count(1) from shop where 1=1 ");
        List param = new ArrayList();
        //判断name是否有数据，如果有，则拼接
        String keyword = map.get("keyword");
        if(keyword!=null && !"".equals(keyword)){
            stb.append(" and shopName like ? ");
            param.add("%"+keyword+"%");
        }
        //判断状态
        String shopStatus = map.get("shopStatus");
        System.out.println(shopStatus);
        if(shopStatus!=null && !"".equals(shopStatus)){
            stb.append(" and shopStatus = ? ");
            param.add(shopStatus);
        }
        //判断手机号
        String phone = map.get("phone");
        if(phone!=null && !"".equals(phone)){
            stb.append(" and shopPhone like ? ");
            param.add("%"+phone+"%");
        }
        //判断日期是否存在s
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if(startTime!=null && !"".equals(startTime)){
            stb.append(" and addTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
        String sql = stb.toString();
        Object object[] = param.toArray();
        System.out.println(sql);
        ResultSet resultSet = JDBCDruidUtil.query(sql, object);
        try {
            if (resultSet.next()){
                count = resultSet.getInt(1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCDruidUtil.close();
        }
        return count;
    }

//    @Override
//    public int updateStatus(Shop shop) {
//        String sql = "update shop set concernStatus=? where shopId=?";
//        Object obj[] = {
//                shop.getConcernStatus(),
//                shop.getShopId()
//        };
//        int row = JDBCDruidUtil.update(sql, obj);
//        return row;
//    }
}
